import { Component} from '@angular/core';
import { NavController,ModalController,ToastController} from 'ionic-angular';
import { PublishSport} from '../publish-sport/publish-sport';

import { Geolocation } from '@ionic-native/geolocation';
import {Login} from "../login/login";
import {StorageService} from "../../providers/StorageService";

import {SportService} from "../../providers/SportService";
import {SportModel} from "../../model/SportModel";
import {USERFACE_PATH,SPORT_BG_IMG_PATH} from "../../providers/Constants";
import {BaseUI} from "../../common/baseui";

import {SportDetailPage} from "../sport-detail/sport-detail";
import {LOCAL_SPORT_TYPE_IMG_PATH} from "../../providers/Constants";
import {CheckUtil} from "../../common/CheckUtil";

@Component({
  selector: 'page-sport',
  templateUrl: 'sport.html'
})

export class SportPage extends  BaseUI{
	pages: Array<{title: string, component: any}>;
   // sportList:any;
  userFacePath:any;
  sportImgPath:string;
  pageNum:any=1;
  pageSize:any=4;
  sportList:any;
  sportTypeId:any=10;
  sortCondition:any=10;
  localSportTypeImgPath:string;

  isLoadEnd:boolean=false;

  lastNote:string=null;
  sportListIsNull:boolean;

  constructor(public navCtrl: NavController,
              public modalCtrl: ModalController,
              private geolocation: Geolocation,
              public storageService:StorageService,
              public sportServiec : SportService,
              public sportModel :SportModel,
              public toastCtrl:ToastController,
              public checkUtil:CheckUtil) {
    super();
    this.userFacePath = USERFACE_PATH;
    this.sportImgPath = SPORT_BG_IMG_PATH;
    this.localSportTypeImgPath = LOCAL_SPORT_TYPE_IMG_PATH;

      // this.pageSize=1;

  }

 //打开约运动界面
  openPublishSport(){

    if(this.checkUtil.checkUserLogin()){
      this.navCtrl.push(PublishSport);
    }else {
      this.navCtrl.push(Login,{page:"PublishSport"});//跳转登录
    }
  }
  ionViewDidEnter(){
    console.log("来了");
    this.pageNum=1;
    this.lastNote=null;//初始化提示语
    this.isLoadEnd=false;//初始化加载完成状态
    this.getAllSports(this.pageNum);
  }

  getAllSports(pageNum){
      this.sportServiec.getSportList(pageNum,this.pageSize,this.sportTypeId,this.sortCondition).subscribe(
        res=>{
          this.sportList=res["data"];
          this.sportListIsNull=(this.sportList.length==0);
          console.log(this.sportList.length==0);
        },error =>{
          // this.toastCtrl.create({message:"网络请求失败", duration: 4500}).present();
          super.showToast(this.toastCtrl,"网络请求失败",'middle');
        } );
  }

  //刷新
  doRefresh(refresher) {
    this.pageNum=1;
    this.lastNote=null;//初始化提示语
    this.isLoadEnd=false;//初始化加载完成状态
    setTimeout(() => {
      this.getAllSports(1);
      refresher.complete();
    }, 2000);
  }

  //加载
  doInfinite(infiniteScroll) {
    console.log(this.isLoadEnd);
    if(this.isLoadEnd){
      infiniteScroll.complete();
      return null;//如果已经到底，就不再发送请求
    }
    setTimeout(() => {
      this.sportServiec.getSportList(++this.pageNum,this.pageSize,this.sportTypeId,this.sortCondition).subscribe(
        res=>{
          console.log("data:"+res["data"]);
          this.sportList=this.sportList.concat(res["data"]);
          if(res["data"].length==0||res["data"]==""){
            this.lastNote="没有更多的数据了";
            this.isLoadEnd=true;
          }
          console.log(this.sportList);
        });

      console.log('Async operation has ended');
      infiniteScroll.complete();
    }, 500);
  }

  //打开运动详情
  goToSportDetail(id){
    if(this.checkUtil.checkUserLogin()){
      this.navCtrl.push(SportDetailPage,{id:id});
    }else {
      this.navCtrl.push(Login,{page:"PublishSport"});//跳转登录
    }
  }

  //筛选及排序活动
  select(){
    this.pageNum=1;
    this.isLoadEnd=false;
    this.getAllSports(this.pageNum);
  }


}
